# -*- coding: UTF-8 -*-

# Copyright (c) 2007 Daniele Favara <nomed@dsslive.org>.
#
# This is free software you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# This software is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING.  If not, write to the Free
# Software Foundation, Inc., 51 Franilin St, Fifth Floor, Boston, MA

from tginvoice.model import tables

class Obj(object):
    def __init__(self, Orig, fobjs,
                 *args, **kw):
        self._keys = []
        self._fobjs = fobjs
        self._id = id
        self.Orig = Orig
        for key in Orig.c.keys():
            if not getattr(Orig.c, key).primary_key:
                setattr(self, key, None)
            self._keys.append(key)
        for key, value in kw.iteritems():
            setattr(self, key, value)
        for key, value in self._fobjs.iteritems():
            setattr(self, key, value)

    def keys(self):
        return self._keys

    def fobjs(self):
        return self._fobjs.keys()

    def save(self):
        pass



class ProformaObj(Obj):
    def save(self):
        orig = self.Orig()
        for key in self.keys():
            if key != 'invoice_id':
                setattr(orig, key, getattr(self, key))
        for key in self.fobjs():
                if key != 'products':
                    if hasattr(self.Orig.c, key):
                        if not getattr(self.Orig.c, key).primary_key:
                            setattr(orig, key, getattr(self, key))
                    else:
                        setattr(orig, key, getattr(self, key))
                elif key == 'products':
                    for p in self.products:
                        orig.products.append(p.save())

        return orig

class InvoiceObj(ProformaObj):
    """
      InvoiceObj(tables.Invoice,
                  {'products':[],
                   'biller': None,
                   'customer': None})
    """
    pass

class ProductObj(Obj):
    def save(self):
        orig = self.Orig()
        for key in self.keys():
            if not getattr(self.Orig.c, key).primary_key:
                setattr(orig, key, getattr(self, key))
        return orig

from formencode import FancyValidator, Schema, validators


class Customer(Schema):
    vat_code = validators.UnicodeString(not_empty=True)
    fiscal_code = validators.UnicodeString()
    company_name = validators.UnicodeString(not_empty=True)
    company_description = validators.UnicodeString()
    street = validators.UnicodeString()
    city = validators.UnicodeString()
    state_province = validators.StateProvince()
    country = validators.UnicodeString()
    web = validators.URL(add_http=True, check_exists=False)
    phone = validators.UnicodeString()
    fax = validators.UnicodeString()
    post_code = validators.PostalCode()

class InvoiceProduct(Schema):
    description = validators.UnicodeString(not_empty=True)
    note = validators.UnicodeString()
    unit_price = validators.Number(not_empty=True)
    vat = validators.Number(not_empty=True)
    qty = validators.Number(not_empty=True)

class Project(Schema):
    name = validators.UnicodeString(not_empty=True, strip=True)
    description = validators.UnicodeString(not_empty=True)
    customer_id = validators.Number()
    user_id = validators.Number(not_empty=True)

class Task(Schema):
    milestone_id = validators.Number(not_empty=True)
    title = validators.UnicodeString(not_empty=True, strip=True)
    description = validators.UnicodeString(not_empty=True)
    priority = validators.UnicodeString(not_empty=True)

class Milestone(Schema):
    name = validators.UnicodeString(not_empty=True, strip=True)
    description = validators.UnicodeString(not_empty=True)
    enddate = begindate = validators.DateConverter(datetime_module='datetime')

class Created(Schema):
    created = validators.DateConverter(datetime_module='datetime')
